layui.use(['layer', 'form', 'admin', 'ax','upload'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;
    var upload = layui.upload;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();



    //普通图片上传
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: Feng.ctxPath + '/leimu/img'
        ,before: function(obj){//文件上传前的回调
            //预读本地文件示例，不支持ie8
            console.log(obj);
            obj.preview(function(index, file, result){
                $('#demo1').attr('src', result); //图片链接（base64）直接将图片地址赋值给img的src属性
            });
        }
    });


    //加载下拉框
    $.ajax({
        url: Feng.ctxPath + '/leimu/upLevelList',
        async:false,
        success: function (data) {
            $(data).each(function () {
                $("#uplevelId").append(new Option(this.name,this.id,true));
            });
        }
    });
    form.render('select');
    //加载结束



    //获取类目信息
    var ajax = new $ax(Feng.ctxPath + "/leimu/findId?id=" + Feng.getUrlParam("id"),function (data) {
        var lev=data.level;
        if (lev==2){
            $("#Uple").show();
        } else {
            $("#Uple").hide();
        }
        //图片
        var src =  data.icon.split("/");
        $('#demo1').attr("src",src[1]);
    });


    //获取类目级别进行判定显示
    var result = ajax.start();
    form.val('leimuForm',result);


    //调整类目级别
    form.on('select(test)',function (data) {
        var c=data.value;
        if (c==2){
            $("#Uple").show();
        }else{
            $("#Uple").hide();
        }
    });


    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/leimu/edit", function (data) {
            Feng.success("修改成功!");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(data.field);
        ajax.start();
    });
});